import request from '@/utils/request'

//excel导出请求
let baseServices = "/system/utils";

//===========================================一些常用平台相关请求======================
/**
 * 批量获取多个字典值
 * @param {*} dictTypeList 传入[t1,t2]格式
 * @returns {key:'字典类型',vlaue:'字典集合'}
 * @author：mashuai
 */
export function getDictBatch(dictTypeList) {
  return request({
    url: baseServices + '/dict',
    method: 'post',
    data: dictTypeList
  }).then(res => {
    if (res && res.code === 200) {
      let data = res.data;
      return data;
    } else {
      return {}
    }
  })
}

/**
 * 根据附近id初始化图片---代做
 * @param {*} fileIds [文件id集合，一次性获取] 
 * @author：mashuai
 */
export function initFileList(fileIds) {
  return request({
    url: baseServices + '/getFile',
    method: 'post',
    data: fileIds
  }).then(res => {
    if (res && res.code === 200) {
      let data = res.data;
      return data;
    } else {
      return {}
    }
  });
}


//=============================================crud导入导出通用接口===================================
/**
 * 
 * 获取表里的最大顺利号
 * @param {*} targetParam 
 * @param {必填} condition   {"name@like":"","buildingId@eq":"b1","id@in":["b1f1r1","b1f1r2"]},
 *  @author：mashuai
 */
export function getTbMaxSeqNo(targetParam, condition) {
  return request({
      url: baseServices + '/tbMaxSeqNo',
      method: 'post',
      data: {
        targetParam: targetParam,
        data: condition,
      }
    }).then(res => {
      return res.data;
    })
    .catch(error => {
      return -1;
    });
}

/**
 * 
 * 表的某个字段值是否重复
 * @param {*} targetParam 
 * @param {必填} condition   {"name@like":"","buildingId@eq":"b1","id@in":["b1f1r1","b1f1r2"]},
 *  @author：mashuai
 */

export function checkFieldVal(targetParam, fieldName, fieldVal, condition) {
  return request({
      url: baseServices + '/checkFieldVal',
      method: 'post',
      data: {
        targetParam: targetParam,
        data: condition,
        other: {
          "fieldName": fieldName,
          "fieldVal": fieldVal
        }
      }
    }).then(res => {
      return res.data.data;
    })
    .catch(error => {
      return -1;
    });
}

/**
 * 通用的列表查、分页查询
 * @param {必填} targetParam  "@CrudAnnotation注解注解里的值",
 * @param {必填} data   {"name@like":"","buildingId@eq":"b1","id@in":["b1f1r1","b1f1r2"]},
 * @param {非必填} rewriteMethod  "默认方法不满足需求，重写"
 * @param {非必填} config 如果分页，传入{"curPage":1,"pageSize":1},
 * @author mashuai
 */
export function findList(targetParam, data, rewriteMethod, config) {
  return request({
    url: baseServices + '/find',
    method: 'post',
    data: {
      targetParam: targetParam,
      data: data,
      rewriteMethod: rewriteMethod,
      config: config
    }
  })
}


/**
 * 把某一张表数据变成下拉框值
 * @param {必填} targetParam 
 * @param {必填} data   {"name@like":"","buildingId@eq":"b1","id@in":["b1f1r1","b1f1r2"]},
 * @param {非必填} rewriteMethod  "默认方法不满足需求，重写"
 * @param {非必填}config {"limit":"截取","desc":"降序的字段名逗号拼接","asc":"升序的字段名逗号拼接"}
 * @author mashuai
 */
export function tbDict(targetParam, data, config, rewriteMethod) {
  return request({
    url: baseServices + '/tbDict',
    method: 'post',
    data: {
      targetParam: targetParam,
      data: data,
      rewriteMethod: rewriteMethod,
      config: config
    }
  }).then(res => {
    return res.data;
  })
}

/**
 * 单表删除、包含删除引用校验
 * @param {必填} targetParam 
 * @param {必填} data   {"list":[删除数据的id集合]},
 * @param {非必填} rewriteMethod  "默认方法不满足需求，重写"
 * @param {非必填} config ：{"throw":"默认true,如果删除值被其他表引用了，提示的异常信息，值为false时候，会把没有被引用的数据进行删除，返回被引用数据的详情信息与 MsDelCheckDto ", "check":"默认true",根据实体类配置的，进行数据引用校验，值为false时候，直接删除集合内所有的数据。}
 * @author mashuai
 */
export function tbDel(targetParam, data, rewriteMethod, config) {
  return request({
    url: baseServices + '/delete',
    method: 'post',
    data: {
      targetParam: targetParam,
      data: data,
      rewriteMethod: rewriteMethod,
      config: config
    }
  })
}
/**
 * 通用的列表查、分页查询
 * @param {必填} targetParam  "@CrudAnnotation注解注解里的值",
 * @param {必填} data   {实体类数据},
 * @param {非必填} rewriteMethod  "默认方法不满足需求，重写"
 * @param {非必填} config  {"onAdd":"true/false","check":"true/false"}
 * @author mashuai
 */
export function saveData(targetParam, data, rewriteMethod, config) {
  return request({
    url: baseServices + '/save',
    method: 'post',
    data: {
      targetParam: targetParam,
      data: data,
      rewriteMethod: rewriteMethod,
      config: config
    }
  })
}
//=============================================excel导入导出通用接口===================================

/**
 * 模板导出，自己定义好导出模板后，渲染数据
 * @param {List} dataList   导出的数据，
 * @param {Map} otherVal 其他值，可以是条件，也可以是本次需要携带数据等 
 * @param {*} templatePath 必填  导出excel的模板
 * @param {Map} config 
 *   "config":{
        "watermark":"水印xxx",                    //水印
        "targetParam":"className@template",       //重写时候的参数，比如后台类叫ServiceClass，那么类上需要加@MsExcelRewrite(targetParam="className")，重写的方法加上@MsExcelRewrite(targetParam="template")如下 public void m(List data,Map<String,Object> otherVal ),
        "mergeColIndex":[0,1],                    //需要合并的列，这里合并第一第二列，下面两个值仅仅在mergeColIndex不为空成立
        "excludeRowIndex":[0],                    //在进行列合并时候，排除哪些行
        "excludeTillRow":true                     //是否排除第一行
    } 
 * 
 * @param {*} fileName   导出后，需要下载的文件名，不填以时间戳命令
 *  @author mashuai
 */
export function templateExport(dataList, templatePath, config, otherVal, fileName) {
  return request({
    url: baseServices + '/templateExport',
    method: 'POST',
    data: {
      dataList: dataList,
      templatePath: templatePath,
      config: config,
      otherVal: otherVal
    },
    responseType: "blob"
  }).then(res => {
    var blob = new Blob([res.data]);
    var url = window.URL.createObjectURL(blob);
    var a = document.createElement("a");
    a.download = !fileName ? (new Date().getTime()) + ".xlsx" : fileName + ".xlsx";
    a.href = url;
    a.click();
  });
}
/**
 * 动态列导出
 * @param {List} dataList 必填  导出的数据
 * @param {List} headList  导出数据的列头，不满足可以在后台自己拦截进行处理
 * @param {Map} config  
 *  "config":{
      "watermark":"水印",
      "mergeColIndex":[0,1],                //合并的列
      "targetParam":"className@dynamic"     //导出前进行数据拦截，对数据进行处理 ，public void m (List data,List<String> headers)
    }
 * @param {*} params 一些查询条件等参数
 * @param {*} fileName   导出后，需要下载的文件名，不填以时间戳命令
 *  @author mashuai
 */
export function dynamicExport(dataList, headList, config, params, fileName) {
  return request({
    url: baseServices + '/dynaminExport',
    data: {
        dataList: dataList,
        headList: headList,
        config: config,
        params: params
    },
    responseType: "blob"
}).then(res => {
    var blob = new Blob([res.data]);
    var url = window.URL.createObjectURL(blob);
    var a = document.createElement("a");
    a.download = !fileName ? (new Date().getTime()) + ".xlsx" : fileName + ".xlsx";
    a.href = url;
    a.click();
});
}


/**
 *  解析excel数据。规范起见，每个需要解析度字段都要在实体中进行绑定
 * @param {*} file  上传的文件
 * @param {*} targetParam  必填 如果你需要对数据进行额外处理，就必填，格式selfServiceClass@handleNormalData 
 * @param {*} headRow 必填 从第几行开始读
 *  @author mashuai
 */
export function readExcel(file, targetParam, headRow, remark) {
  return axios({
    method: "POST",
    url: baseServices + '/readExcel',
    headers: {
        'Content-Type': 'multipart/form-data',
        Token: localStorage.logintoken
    },
    data: {
        file: file,
        targetParam: targetParam,
        headRow: headRow,
        remark:remark
    },
});
}




//=============================================文件通用接口===================================
//下载java后端resource目录下的文件
/**
 * 参数
 * @param {*} path /static/enterprise_individual_apply.xlsx
 * @param {*} suffix xlsx
 * @param {*} fileName  下载的文件名字
 */
export function downloadFileByResource(path, suffix, fileName) {
  return request({
    method: "POST",
    url: baseServices + '/downloadFile',
    data: {
        path: path,
        suffix: suffix,
        pathType: "1"
    },
    responseType: "blob"
}).then(res => {
    var blob = new Blob([res.data]);
    var url = window.URL.createObjectURL(blob);
    var a = document.createElement("a");
    a.download = !fileName ? (new Date().getTime()) + "." + suffix : fileName + "." + suffix;
    a.href = url;
    a.click();
});
}
